package by.averianov.discounta.webapp.page.create;

import javax.inject.Inject;

import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.PageCreator;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.ResourceModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import by.averianov.discounta.datamodel.PriceBook;
import by.averianov.discounta.datamodel.UserProfile;
import by.averianov.discounta.services.EconomistService;
import by.averianov.discounta.services.UserService;
import by.averianov.discounta.webapp.app.BasicAuthenticationSession;
import by.averianov.discounta.webapp.page.BaseLayout;
import by.averianov.discounta.webapp.page.homepage.HomePage;
import by.averianov.discounta.webapp.page.pricebook.Economist;

public class CreateDiscount extends BaseLayout {

	private static final Logger LOGGER = LoggerFactory.getLogger(CreateDiscount.class);
	
	private Long id;
	private Long valueSumLimit;
	private Long valuePercentDiscount;
	private UserProfile userEditor;
	private String userEditorEmail;
	
	private UserProfile sessionProfile;
	
	private PageCreator pageCreator;
	
	public CreateDiscount(PriceBook priceBook, PageCreator pageCreator) {
		super(null);		
		this.pageCreator = pageCreator;
		
		try {
			this.id = priceBook.getId();
			this.valueSumLimit = priceBook.getValueSumLimit();
			this.valuePercentDiscount = priceBook.getValuePercentDiscount();
			this.userEditor = priceBook.getUserEditor();			
			LOGGER.debug("Changing a user");
			this.userEditorEmail = userService.getUserAccount(this.userEditor.getId()).getUserEmail();
		} catch (NullPointerException e) {
			LOGGER.debug("Creating a user, card is " + e.getMessage());
			this.valueSumLimit = 0L;
			this.valuePercentDiscount = 0L;
		}
	}
	
	@Inject
	private EconomistService economistService;
	@Inject
	private UserService userService;
	
	
	@Override
	protected void onInitialize() {
		super.onInitialize();
		if(BasicAuthenticationSession.get().isSignedIn()) {
			this.sessionProfile = BasicAuthenticationSession.get().getUserProfile();
		} else {
			HomePage page = new HomePage();
			page.error(new ResourceModel("p.error.nouserlogin").getObject());
			setResponsePage(page);			
		}
		LOGGER.debug("Session User From CreatCard: " + sessionProfile);
		
		final Label labelPriceBookId = new Label("labelPriceBookId",  Model.of(id));		
		final TextField<Long> valueSumLimitText = new TextField<Long>("valueSumLimit", new Model<Long>(valueSumLimit));
		valueSumLimitText.setType(Long.class);
		final TextField<Long> valuePercentText = new TextField<Long>("valuePercentDiscount", new Model<Long>(valuePercentDiscount));
		valuePercentText.setType(Long.class);
		final Label userEditorText = new Label("userEditor", Model.of(userEditorEmail));
		
		Form<?> form = new Form<Void>("priceBookCreateForm") {
			@Override
			protected void onSubmit() {
				
				PriceBook resultingPriceBook = new PriceBook();
				resultingPriceBook.setId(id);
				Long resultingValueSumLimitText = valueSumLimitText.getModelObject();
				Long resultingValuePercentText = valuePercentText.getModelObject();
				if (resultingValueSumLimitText != null|| resultingValuePercentText != null) {
			        resultingPriceBook.setValueSumLimit(resultingValueSumLimitText);
			        resultingPriceBook.setValuePercentDiscount(resultingValuePercentText);
			        resultingPriceBook.setUserEditor(sessionProfile);
			        economistService.saveOrUpdatePriceBook(resultingPriceBook);
			        setResponsePage(pageCreator.createPage());
				} else {
					Economist page = new Economist(2, 0L);
					page.error(new ResourceModel("p.error.nosetvalue").getObject());
					setResponsePage(page);
				}
		        
				
			}
		};		 
		add(form);
				
		Label labelCreatePriceBook = new Label("labelCreatePriceBook", getLabelCreatePriceBook(id));
		form.add(labelCreatePriceBook);
		
		Label labelPriceBookIdText = new Label("labelPriceBookIdText", getLabelPriceBookIdText());
		if(id == null){
			labelPriceBookIdText = new Label("labelPriceBookIdText", "");
		}
		form.add(labelPriceBookIdText);
		
		
		form.add(labelPriceBookId);		
		form.add(valueSumLimitText);
		form.add(valuePercentText);
		form.add(userEditorText);
		
		form.add(new Label("labelSave", getButtonSave()));
		
		Link linkCancel = new Link("linkCancel"){
			@Override
			public void onClick() {
				setResponsePage(pageCreator.createPage());
				
			}
		};
		linkCancel.add(new Label("labelCancel", getButtonCancel()));
		form.add(linkCancel);
	}
	
}
